package moro;

/**
 * Title:
 * Description:  This window shows the entire environment and the robot moving in it.
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author
 * @version 1.0
 */

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;

@SuppressWarnings("serial")
public class SimulWindow extends JFrame {
	Environment environment = null;
	JPanel contentPane;
	JMenuBar menuBar = new JMenuBar();
	JMenu menuFile = new JMenu();
        JMenuItem menuFileOpenMap = new JMenuItem();
	JMenuItem menuFileExit = new JMenuItem();
        
	public void redraw() {
		environment.repaint();
	}

        /**Construct the frame
         * @param p_environment 
         */
	public SimulWindow(Environment p_environment) {
		environment = p_environment;
		// The call to enableEvents(..) in combination with the processWindowEvent(..)
		// is a really old fashioned way to close the window. Just ignore it.
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);

		// ------------------------- contentPane -----------------------------------
		contentPane = (JPanel) this.getContentPane();
		contentPane.setLayout(new BorderLayout());
		this.setTitle("Mobile Robot Environment");

		// --------------------------- Menu ----------------------------------------
		// Menu File
		menuFile.setText("File");
		// Menu File Open Map
		menuFileOpenMap.setText("Open Map");
		menuFileOpenMap.addActionListener(new ActionListener()  {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        menuFileOpenMap_actionPerformed(e);                    
                    }
		});
		// Menu File Exit
		menuFileExit.setText("Exit");
		menuFileExit.addActionListener(new ActionListener()  {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        menuFileExit_actionPerformed(e);
                    }
		});
		// Adds the menu components
		menuFile.add(menuFileOpenMap);
		menuFile.add(menuFileExit);
		menuBar.add(menuFile);
               
		// --------------------------- Panels --------------------------------------
		setBounds(0, 0, 510, 460);
		environment.setBackground(SystemColor.window);
		this.getContentPane().add(environment);
		this.setJMenuBar(menuBar);
	}

	// --------------------------- Menu actions ----------------------------------
	/** Menu File Open Map*/
	void menuFileOpenMap_actionPerformed(ActionEvent e) {
		JFileChooser chooser = new JFileChooser(new File(System.getProperty("user.dir")));//Start in application directory
		FileFilter filter = new ExtensionFileFilter("XML files", "XML");//Initial filefilter shows only XML-files. Show all files option is available
		chooser.setFileFilter(filter);
		int returnVal = chooser.showOpenDialog(this);
		if(returnVal == JFileChooser.APPROVE_OPTION){
                    if(environment.loadMap(chooser.getSelectedFile())) {
                        environment.repaint();
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "This is not a valid Map file!");
                    }
		}
	}

        /**File | Exit action performed
         * @param e 
         */
	public void menuFileExit_actionPerformed(ActionEvent e) {
		System.exit(0);
	}

	/**Overridden so we can exit when window is closed*/
        @Override
	protected void processWindowEvent(WindowEvent e) {
		super.processWindowEvent(e);
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
                    menuFileExit_actionPerformed(null);
		}
	}
               /**File | Exit action performed
         * @param e 
         */
	public void menuAboutExit_actionPerformed(ActionEvent e) {
		//TODO Add an action here;
	}
}
